<?php
/** @noinspection PhpUnused */
declare(strict_types=1);

namespace app\shop\controller;

use app\shop\exception\FailException;
use app\shop\exception\ParamsErrorException;
use app\shop\logic\GoodsLogic;
use app\shop\validate\GoodsValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

/**
 * @property GoodsLogic $logic
 */
class GoodsController extends BaseController
{
    protected string $validateClassName = GoodsValidate::class;

    protected array $noNeedLogin = ['index', 'detail', 'classify', 'topClassify', 'classifyDetail'];

    public function __construct(GoodsLogic $goodsLogic)
    {
        parent::__construct();

        $this->logic = $goodsLogic;
    }

    /**
     * 商品列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function index(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'list');

        $result = $this->logic->list($params);

        return $this->success($result);
    }

    /**
     * 商品详情
     * @return Json
     * @throws ParamsErrorException
     * @throws FailException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'detail');

        $result = $this->logic->detail($params);

        return $this->success($result);
    }

    /**
     * 商品分类
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws ParamsErrorException
     */
    public function classify(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'classify');

        $result = $this->logic->getAllClassify($params);

        return $this->success($result);
    }

    /**
     * 商品一级分类
     * @return Json
     */
    public function topClassify(): Json
    {
        $result = $this->logic->topClassify();

        return $this->success($result);
    }

    /**
     * 分类详情
     * @return Json
     * @throws ParamsErrorException
     */
    public function classifyDetail(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'classify_detail');

        $result = $this->logic->getGoodsListByClassify($params);

        return $this->success($result);
    }

    /**
     * 商品服务与承诺
     * @return Json
     * @throws ParamsErrorException
     */
    public function service(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'service');

        $result = $this->logic->getServiceList($params);

        return $this->success($result);
    }

    /**
     * 分享
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function share(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'share');

        $result = $this->logic->shareInfo($params);

        return $this->success($result);
    }
}
